package com.gomtel.las.theme1;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

	String TAG = "gomtel";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final IntentFilter filter = new IntentFilter();
		// 屏幕灭屏广播  
		filter.addAction(Intent.ACTION_SCREEN_OFF);
		// 屏幕亮屏广播  
		filter.addAction(Intent.ACTION_SCREEN_ON);
		// 屏幕解锁广播  
		filter.addAction(Intent.ACTION_USER_PRESENT);
		// 当长按电源键弹出“关机”对话或者锁屏时系统会发出这个广播  
		// example：有时候会用到系统对话框，权限可能很高，会覆盖在锁屏界面或者“关机”对话框之上，  
		// 所以监听这个广播，当收到时就隐藏自己的对话，如点击pad右下角部分弹出的对话框   
		filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);

		registerReceiver(mBatInfoReceiver, filter);
		Log.d(TAG, "registerReceiver");
	}

	BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(final Context context, final Intent intent) {
			Log.d(TAG, "onReceive");
			String action = intent.getAction();

			if (Intent.ACTION_SCREEN_ON.equals(action)) {
				Log.d(TAG, "screen on");
			} else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
				Log.d(TAG, "screen off");
			} else if (Intent.ACTION_USER_PRESENT.equals(action)) {
				Log.d(TAG, "screen unlock");
			} else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
				Log.i(TAG, " receive Intent.ACTION_CLOSE_SYSTEM_DIALOGS");
			}
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();

		if (mBatInfoReceiver != null) unregisterReceiver(mBatInfoReceiver);
	}
}
